#include "Controller.h"
#include "Button.h"
#include <CommCtrl.h>

Controller::Controller()
{
}
Controller::~Controller()
{

}
Controller* Controller::GetController(HWND a_hWnd)
{ 
	return (Controller*)GetWindowLong(a_hWnd, GWL_USERDATA);
}
void Controller::SetController(HWND a_hWnd, Controller* a_pController)
{ 
	SetWindowLong(a_hWnd, GWL_USERDATA, (LONG)a_pController); 
}
LRESULT CALLBACK Controller::WndProc(HWND a_hWnd, UINT a_msg, WPARAM a_wParam, LPARAM a_lParam)
{
	//get class pointer
	Controller* pController = GetController(a_hWnd);

	switch(a_msg)
	{
	case WM_NCCREATE:
		{
			pController = (Controller*)(((CREATESTRUCT*)a_lParam)->lpCreateParams);

			if(pController == NULL)
				return false;

			SetController(a_hWnd, pController);
		}
		break;
	case WM_KEYDOWN:
		{
			if(pController)
				pController->KeyDown(a_hWnd, a_wParam);
		}
		break;
	case WM_ACTIVATE:
		{
			if(pController)
				pController->Activate(a_wParam);
		}
		break;
	case WM_SIZE:
		{
			if(pController)
				pController->Size(LOWORD(a_wParam), HIWORD(a_wParam), a_lParam);
		}
		break;
	case WM_LBUTTONDOWN:
		{
			if(pController)
				pController->LButtonDown(a_wParam, LOWORD(a_lParam), HIWORD(a_wParam));
		}
		break;
	case WM_LBUTTONUP:
		{
			if(pController)
				pController->LButtonUp(a_wParam, LOWORD(a_lParam), HIWORD(a_lParam));
		}
		break;
	case WM_MOUSEMOVE:
		{
			if(pController)
				pController->MouseMove(a_wParam, a_lParam);
		}
		break;
	case WM_COMMAND:
		{
			if(pController)
			{
				pController->Command(LOWORD(a_wParam), HIWORD(a_wParam), a_lParam);
				if(HIWORD(a_wParam) == BN_CLICKED)
				{
					pController->ButtonPress((HWND)a_lParam);
				}
			}
		}
		break;
	case WM_NOTIFY:
		{
			if(pController)
				pController->Notify((WPARAM)a_wParam, a_lParam);
		}
		break;
	case WM_PAINT:
		{
			if(pController)
				pController->Paint(a_hWnd);
		}
		break;
	case WM_DRAWITEM:
		{
			if(pController)
				pController->DrawItem(a_wParam, a_lParam);
		}
		break;
	case WM_CLOSE:
		{
			if(pController)
				pController->Close();
		}
		break;
	case WM_DESTROY:
		{
			if(pController)
				pController->Destroy();
		}
		break;
	case WM_CREATE:
		{
			if(pController)
				pController->Create();
		}
		break;
	case WM_RBUTTONDOWN:
		{
			if(pController)
				pController->RButtonDown(a_wParam, LOWORD(a_lParam), HIWORD(a_lParam));
		}		
		break;
	case WM_RBUTTONUP:
		{
			if(pController)
				pController->RButtonUp(a_wParam, LOWORD(a_lParam), HIWORD(a_lParam));
		}
		break;
	case WM_CONTEXTMENU:
		{
			if(pController)
				pController->ContextMenu((HWND)a_wParam, LOWORD(a_lParam), HIWORD(a_lParam));
		}
		break;
	case WM_ERASEBKGND:
		{
			if(pController)
				pController->EraseBackground(a_hWnd, (HDC)a_wParam);
		}
		break;
	default:
		break;
	};

	//Apply any effects after messages have been handled such as changing the status bar text
	if(pController)
	{
		pController->AfterMessages();
	}

	return DefWindowProc(a_hWnd, a_msg, a_wParam, a_lParam);
}
void Controller::Close()
{
}
void Controller::Command(int a_id, int a_cmd, LPARAM a_msg)
{
}
void Controller::KeyDown(HWND a_hWnd, WPARAM a_wParam)
{
}
void Controller::ContextMenu(HWND a_hWnd, int a_x, int a_y)
{
}
void Controller::Create()
{
}
void Controller::Destroy()
{
}
void Controller::EraseBackground(HWND a_hWnd, HDC a_hdc)
{
}
void Controller::LButtonDown(WPARAM a_state, int a_x, int a_y)
{
}
void Controller::LButtonUp(WPARAM a_state, int a_x, int a_y)
{
}
void Controller::Notify(int a_id, LPARAM a_lParam)
{
}
void Controller::Paint(HWND a_hWnd)
{
}
void Controller::RButtonDown(WPARAM a_wParam, int a_x, int a_y)
{
}
void Controller::RButtonUp(WPARAM a_wParam, int a_x, int a_y)
{
}
void Controller::Size(int a_w, int a_h, LPARAM a_lParam)
{
}
void Controller::DrawItem(WPARAM a_wParam, LPARAM a_lParam)
{
}
void Controller::Activate(WPARAM a_wParam)
{
}
void Controller::MouseMove(WPARAM a_wParam, LPARAM a_lParam)
{
}
void Controller::AfterMessages()
{
}
void Controller::ButtonPress(HWND a_hWnd)
{
}